﻿using System.Windows;
using PermissionGroup = RentIt06WPFClient.UserSession.PermissionGroup;
using Account = RentIt06WPFClient.UserSession.Account;

namespace RentIt06WPFClient
{
    using System.Windows.Input;

    /// <summary>
    /// Interaction logic for MainFrame.xaml
    /// </summary>
    public partial class MainFrame : Window
    {
        private Account account;

        public MainFrame() {
            InitializeComponent();
            Session.Init();
            frame.Navigate(new Login(updateWindow));
            frame.Navigated += removeLoginNavEntry;
        }

        private void removeLoginNavEntry(object sender, System.Windows.Navigation.NavigationEventArgs e) {
            if (this.frame.NavigationService.Content.GetType() == typeof(Login)) return; //navigate is async so incase this gets called before we navigate away from login.
            this.frame.NavigationService.RemoveBackEntry();
            this.nav_back.Visibility = Visibility.Hidden;
            this.frame.Navigated -= this.removeLoginNavEntry;
        }

        private void updateWindow(Account acc)
        {
            this.account = acc;
            switch (acc.Permission)
            {
                case PermissionGroup.Admin:
                    adminButton.Visibility = Visibility.Visible;
                    goto case PermissionGroup.Publisher;
                case PermissionGroup.Publisher:
                    publisherButton.Visibility = Visibility.Visible;
                    break;
            }
            clientButton.Visibility = Visibility.Visible;
            logoutButton.Visibility = Visibility.Visible;
            accountButton.Visibility = Visibility.Visible;
        }

        private void clientButton_Click(object sender, RoutedEventArgs e)
        {
            frame.Navigate(new FrontPage());
        }

        private void publisherButton_Click(object sender, RoutedEventArgs e)
        {
            frame.Navigate(new ManageMedia());
        }

        private void adminButton_Click(object sender, RoutedEventArgs e)
        {
            frame.Navigate(new ListUsers());
        }

        private void logoutButton_Click(object sender, RoutedEventArgs e)
        {
            switch (account.Permission)
            {
                case PermissionGroup.Admin:
                    Session.Admin.Close();
                    goto case PermissionGroup.Publisher;
                case PermissionGroup.Publisher:
                    Session.Publisher.Logout();
                    Session.Publisher.Close();
                    break;
            }
            Session.User.Logout();
            Session.User.Close();
            Application.Current.Shutdown();
        }

        private void accountButton_Click(object sender, RoutedEventArgs e)
        {
            frame.Navigate(new EditUser());
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {

        }

        private void keyUp(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Back && frame.NavigationService.CanGoBack && !frame.IsFocused) frame.NavigationService.GoBack();
            //This is a quick fix that does not navigate on alt+arrows, mouse back button etc.
            //It also happens to go back when the user tries to delete text in a text box.
        }

        private void nav_back_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            frame.GoBack();
        }

        private void nav_forward_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            frame.GoForward();
        }

        private void frame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) {
            nav_back.Visibility = frame.NavigationService.CanGoBack ? Visibility.Visible : Visibility.Hidden;
            nav_forward.Visibility = frame.NavigationService.CanGoForward ? Visibility.Visible : Visibility.Hidden;
        }
    }
}
